package com.lancoo.cvas.aspect;

/**
 * @ClassName CacheRemoveAspect
 * @Description 功能描述:清除缓存切面类
 * @Author quin
 * @Date 2023/2/15 16:40
 */
//@Component
//@Aspect
//@Slf4j
//public class CacheRemoveAspect {
//
//    @Resource
//    RedisTemplate<String, String> redis;
//    @Resource
//    private TeachingClassService teachingClassService;
//
//    //截获标有@CacheRemove的方法
//    @Pointcut(value = "(@annotation(com.lancoo.cvas.aspect.CacheRemove))")
//    private void pointcut() {
//    }
//
//    /**
//     * 功能描述: 切面在截获方法返回值之后
//     */
//    @AfterReturning(value = "pointcut()")
//    private void process(JoinPoint joinPoint) {
//        //获取被代理的类
//        Object target = joinPoint.getTarget();
//        //获取切入方法的数据
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        //获取切入方法
//        Method method = signature.getMethod();
//        //获得注解
//        CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
//
//        if (cacheRemove != null) {
//            String[] keys = cacheRemove.key();
//            Long studentId = (Long) getValueByName(joinPoint, "studentId");
//            Long teachingClassId = (Long) getValueByName(joinPoint, "teachingClassId");
//            TeachingClass teachingClass = teachingClassService.getById(teachingClassId);
//            for (String key : keys) {
//                String redisKey;
//                switch (key) {
//                    case "student:course":
//                        redisKey = "cvas:student:course::" + studentId + "-" + teachingClass.getTeachingClassType();
//                        break;
//                    case "student:require":
//                        redisKey = "cvas:student:require::" + studentId + "-" + teachingClass.getStudyNature();
//                        break;
//                    default:
//                        return;
//                }
//                log.info(redisKey);
//                redis.delete(redisKey);
//            }
////            Object[] paramValues = joinPoint.getArgs();
////            String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
////            for (int i = 0; i < paramNames.length; i++) {
////                if("studentId".equals(paramNames[i])){
////                    Long studentId = (Long)paramValues[i];
////                    teachingClassService.getById()
////                    String[] keys = cacheRemove.key();
////                    for (String key : keys) {
////                        //清除缓存
////                        cleanRedisCache("*" + key + "::"+studentId+"*");
////                    }
////                }
////            }
//        }
//    }
//
//    private Object getValueByName(JoinPoint joinPoint, String name) {
//        Object[] paramValues = joinPoint.getArgs();
//        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
//        for (int i = 0; i < paramNames.length; i++) {
//            if (name.equals(paramNames[i])) {
//                return paramValues[i];
//            }
//        }
//        return null;
//    }
//
//    private void cleanRedisCache(String key) {
//        Set<String> stringSet = redis.keys(key);
//        //删除缓存
//        redis.delete(stringSet);
//        log.info("清除 " + key + " 缓存");
//    }
//}
//
